package com.jinkaimen.goodcash.ui.activity;

import android.view.View;
import android.widget.BaseAdapter;

import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseRefreshListViewActivity;
import com.ht.baselibs.utils.DateUtils;
import com.ht.baselibs.utils.UIUtils;
import com.jinkaimen.goodcash.bean.TransactionHistoryBean;
import com.jinkaimen.goodcash.bean.TransactionHistoryItemBean;
import com.jinkaimen.goodcash.dao.AccountDao;
import com.jinkaimen.goodcash.manager.ApiManager;
import com.jinkaimen.goodcash.ui.adapter.TransactionHistoryAdapter;

import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class TransactionHistoryActivity extends BaseRefreshListViewActivity<TransactionHistoryItemBean> {

    private int mPageNo = 1;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("交易记录");
    }

    @Override
    protected View getHeaderView() {

        return null;
    }

    @Override
    protected void initChildDataFromCache() {

    }

    @Override
    protected BaseAdapter getAdapter() {
        return new TransactionHistoryAdapter(this, mDataList);
    }

    @Override
    protected void load() {
        ApiManager.getApi().transactionHistory(
                "HSY001036",
                AccountDao.getInstance().getAccount().accountID,
                mPageNo,
                10,
                DateUtils.formatDate(new Date().getTime())
        ).enqueue(new NetCallBack<TransactionHistoryBean>() {
            @Override
            protected void onSuccess(Call<TransactionHistoryBean> call, Response<TransactionHistoryBean> response) {
                mDataList.addAll(response.body().records);
            }

            @Override
            protected void hiddenProgress() {
                onRefreshCompleteSuccess();
            }
        });
    }

    @Override
    protected void onLoadMore(List<TransactionHistoryItemBean> dataList) {
        mPageNo++;
        ApiManager.getApi().transactionHistory(
                "HSY001036",
                AccountDao.getInstance().getAccount().accountID,
                mPageNo,
                10,
                DateUtils.formatDate(new Date().getTime())
        ).enqueue(new NetCallBack<TransactionHistoryBean>() {
            @Override
            protected void onSuccess(Call<TransactionHistoryBean> call, Response<TransactionHistoryBean> response) {
                if (response.body().records == null || response.body().records.size() <= 0){
                    UIUtils.showToast("没有更多");
                    mPageNo--;
                } else {
                    mDataList.addAll(response.body().records);
                }
            }

            @Override
            protected void hiddenProgress() {
                onLoadMoreComplete();
            }
        });
    }
}
